/*******************************************************************************
																				
	DESCRIPTION: 	This do file calculates basic descriptive statistics about 
					the baseline sample.
	
*******************************************************************************/

clear all
global id_code 106

/****************************************************************************
* Basic statistics for individuals used in the analysis
*****************************************************************************/

* Take the data before replacing missing observations
use "${data}/001_9_LISA_PES_cleaned.dta", clear

* Keep only the age bracket used in the analysis
keep if age>=25 & age<=55

* Merge with training data:
merge 1:1 Lop* InLnr using "${data}/005_UnemploymentCategoryStats", ///
	assert(2 3) keep(3) nogen

tab any_training_old, missing


* Generate dummy for each level of education
gen educ_primary=(EducLevel<=2 & EducLevel!=.)
gen educ_second=(EducLevel<=4 & EducLevel>=3)
gen educ_tert=(EducLevel>4 & EducLevel!=.)

* Generate means of variables
collapse (mean) age Gender foreign L_WageInc_adj L_OtherInc_adj L_FamInc_adj ///
		unemplSpells5Ybefore nEmployers5Y tenure educ* DaysUnemp_2Years DaysUnemp_5Years ///
		DaysOnDI_2Years DaysOnDI_5Years L_layoffRate_L1L2 L_nEmployees_L1L2 ///
	(p25) p25DaysUnemp_2Years = DaysUnemp_2Years p25DaysUnemp_5Years = DaysUnemp_5Years ///
		p25DaysOnDI_2Years = DaysOnDI_2Years p25DaysOnDI_5Years = DaysOnDI_5Years ///
		p25L_layoffRate_L1L2=L_layoffRate_L1L2 p25L_nEmployees_L1L2=L_nEmployees_L1L2 /// 
		p25unemplSpells5Ybefore = unemplSpells5Ybefore p25nEmployers5Y = nEmployers5Y ///
		p25tenure = tenure ///
	(p50) p50DaysUnemp_2Years = DaysUnemp_2Years p50DaysUnemp_5Years = DaysUnemp_5Years ///
		p50DaysOnDI_2Years = DaysOnDI_2Years p50DaysOnDI_5Years = DaysOnDI_5Years ///
		p50L_layoffRate_L1L2=L_layoffRate_L1L2 p50L_nEmployees_L1L2=L_nEmployees_L1L2 ///
		p50unemplSpells5Ybefore = unemplSpells5Ybefore p50nEmployers5Y = nEmployers5Y ///
		p50tenure = tenure ///
	(p75) p75DaysUnemp_2Years = DaysUnemp_2Years p75DaysUnemp_5Years = DaysUnemp_5Years /// 
		p75DaysOnDI_2Years = DaysOnDI_2Years p75DaysOnDI_5Years = DaysOnDI_5Years ///
		p75L_layoffRate_L1L2=L_layoffRate_L1L2 p75L_nEmployees_L1L2=L_nEmployees_L1L2 ///
		p75unemplSpells5Ybefore = unemplSpells5Ybefore p75nEmployers5Y = nEmployers5Y ///
		p75tenure = tenure ///
	(count) total_obs = LopNr_PersonNr (sum) training_obs = any_training_old

* Reshape the data:
xpose, clear varname
rename v1 sampleAnalysis

save "${data}/${id_code}_Basic_Stats_SampleAnalysis.dta", replace

/****************************************************************************
* Basic statistics for the whole population 
*****************************************************************************/

use LopNr_PersonNr Ar Alder Kon Sun2000niva MedbGrEg LopNr_FamId ForvErs LoneInk using "${data}/001_2_Lisa_allYears.dta", clear

* Clean the data
rename Ar year 
rename Alder age

* Gender
rename Kon Gender
destring Gender, replace
replace Gender = Gender - 1
label define Gender 0 "Male" 1 "Female"
label values Gender Gender

* Education Level
gen EducLevel=substr(Sun2000niva,1,1)
destring EducLevel, replace
drop Sun2000niva
replace EducLevel=. if EducLevel==9

* Foreign individual
destring MedbGrEg, replace
gen foreign =.
replace foreign = 0 if MedbGrEg == 0
replace foreign = 1 if MedbGrEg<=11 & MedbGrEg>=1
label define foreigner 0 "Swedish citizen"
label define foreigner 1 "Foreign citizen", add
label values foreign foreigner

* Generate income variables
rename LoneInk WageInc
label var WageInc "gross cash salary"

gen OtherInc = ForvErs - WageInc
label var OtherInc "earned income excl. gross cash salary"

* Income of other family members
bysort LopNr_FamId year: egen FamInc = total(ForvErs)
replace FamInc = FamInc - ForvErs
label var FamInc "Income of other family members"
drop LopNr_FamId

* CPI adjusted income
merge m:1 year using "${data}/Inflation.dta"
drop if _merge ==2
drop _merge

foreach var in WageInc OtherInc FamInc {
	gen `var'_adj=`var'/CPI*100
	drop `var'
}

xtset LopNr_PersonNr year 

foreach var in OtherInc_adj WageInc_adj FamInc_adj {
		gen L_`var' = L.`var'
}


* Keep only the year and age corresponding to the analysis
keep if year>=1992 & year<=2016
keep if age>=25 & age<=55

* Generate dummy for each level of education
gen educ_primary=(EducLevel<=2 & EducLevel!=.)
gen educ_second=(EducLevel<=4 & EducLevel>=3)
gen educ_tert=(EducLevel>4 & EducLevel!=.)

* Generate means of variables
collapse age Gender foreign L_WageInc_adj L_OtherInc_adj L_FamInc_adj  educ*

* Reshape the data
xpose, clear varname
rename v1 sampleAll

save "${data}/${id_code}_Basic_Stats_SampleAll.dta", replace


/****************************************************************************
* Combine the statistics 
*****************************************************************************/

use "${data}/${id_code}_Basic_Stats_SampleAnalysis.dta", clear
merge 1:1 _varname using "${data}/${id_code}_Basic_Stats_SampleAll.dta"
drop _merge
order _varname, first
save "${data}/${id_code}_Basic_Stats_SampleAnalysis_and_SampleAll.dta", replace


/****************************************************************************
* Latex table
*****************************************************************************/

* Put stats into locals:
forval i = 1/`=_N' {
	local  `=_varname[`i']'_Full = `=sampleAnalysis[`i']'
	local `=_varname[`i']'_Pop = `=sampleAll[`i']'
}

foreach x in Gender foreign educ_primary educ_second educ_tert {
	local `x'_Full = ``x'_Full' * 100
	local `x'_Pop = ``x'_Pop' * 100
}

foreach x in L_layoffRate_L1L2 {
	local `x'_Full = ``x'_Full' * 100
	local p25`x'_Full = `p25`x'_Full' * 100
	local p50`x'_Full = `p50`x'_Full' * 100
	local p75`x'_Full = `p75`x'_Full' * 100

}

foreach x in L_WageInc_adj L_OtherInc_adj L_FamInc_adj {
	local `x'_Full = round(``x'_Full', 1) * 100
	local `x'_Pop = round(``x'_Pop', 1) * 100
}



* Write into LaTeX file:
file open myfile using "${output}/${id_code}_BasicStatistics_table.tex", write replace

file write myfile "% \documentclass{article}" _newline ///
	"% \usepackage{booktabs}" _newline ///
	"% \usepackage[margin=1in]{geometry}" _newline ///
	"% \begin{document}" _newline ///
	"% \begin{table}[h]" _newline ///
	"% \begin{center}" _newline ///
	"\begin{tabular}{l @{\extracolsep{16pt}} c @{\extracolsep{2pt}} c c c}" _newline ///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	///
	"\textbf{A. Unemployed Sample vs. Population} & \multicolumn{4}{c}{\textbf{Mean}} \\" _newline ///
	"\cline{2-5}" _newline ///
	" & \multicolumn{2}{c}{Unemployed} & \multicolumn{2}{c}{Population} \\" _newline ///
	"\addlinespace[1.5ex]" _newline ///
	"Age & \multicolumn{2}{c}{`: di %5.1f `age_Full''} & \multicolumn{2}{c}{`: di %5.1f `age_Pop''} \\" _newline ///
	"Female & \multicolumn{2}{c}{`: di %5.0f `Gender_Full''\%} & \multicolumn{2}{c}{`: di %5.0f `Gender_Pop''\%} \\" _newline ///
	"Foreign & \multicolumn{2}{c}{`: di %5.0f `foreign_Full''\%} & \multicolumn{2}{c}{`: di %5.0f `foreign_Pop''\%} \\ \addlinespace[0.2cm]" _newline ///
	"Primary Edu. & \multicolumn{2}{c}{`: di %5.0f `educ_primary_Full''\%} & \multicolumn{2}{c}{`: di %5.0f `educ_primary_Pop''\%} \\" _newline ///
	"Secondary Edu. & \multicolumn{2}{c}{`: di %5.0f `educ_second_Full''\%} & \multicolumn{2}{c}{`: di %5.0f `educ_second_Pop''\%} \\" _newline ///
	"Tertiary Edu. & \multicolumn{2}{c}{`: di %5.0f `educ_tert_Full''\%} & \multicolumn{2}{c}{`: di %5.0f `educ_tert_Pop''\%} \\  \addlinespace[0.2cm]" _newline ///
	"Labour Income (2006 SEK) & \multicolumn{2}{c}{`: di %9.0fc `L_WageInc_adj_Full''} & \multicolumn{2}{c}{`: di %9.0fc `L_WageInc_adj_Pop''} \\" _newline ///
	"Other Income (2006 SEK) & \multicolumn{2}{c}{`: di %9.0fc `L_OtherInc_adj_Full''} & \multicolumn{2}{c}{`: di %9.0fc `L_OtherInc_adj_Pop''} \\" _newline ///
	"Household Income (2006 SEK) & \multicolumn{2}{c}{`: di %9.0fc `L_FamInc_adj_Full''} & \multicolumn{2}{c}{`: di %9.0fc `L_FamInc_adj_Pop''} \\ \addlinespace[0.2cm]" _newline ///
	"Number of unemployment spells & \multicolumn{2}{c}{`: di %9.0fc `total_obs_Full''} & \multicolumn{2}{c}{} \\" _newline ///
	"Spells interrupted by training & \multicolumn{2}{c}{`: di %9.0fc `training_obs_Full''} & \multicolumn{2}{c}{} \\" _newline ///
	"\addlinespace[1.5ex]" _newline ///
	"\hline \addlinespace[1.5ex]" _newline ///
	///
	"\textbf{B. Employment History} & \textbf{Mean} & \multicolumn{3}{c}{\textbf{Percentile}} \\" _newline ///
	"\cline{3-5}" _newline ///
	" & & 25th & 50th & 75th \\" _newline ///
	"\hline \addlinespace[1.5ex]" _newline ///
	"Days on UI (2y) & `: di %5.0f `DaysUnemp_2Years_Full'' & `: di %5.0f `p25DaysUnemp_2Years_Full'' & `: di %5.0f `p50DaysUnemp_2Years_Full'' & `: di %5.0f `p75DaysUnemp_2Years_Full'' \\" _newline ///
	"Days on UI (5y) & `: di %5.0f `DaysUnemp_5Years_Full'' & `: di %5.0f `p25DaysUnemp_5Years_Full'' & `: di %5.0f `p50DaysUnemp_5Years_Full'' & `: di %5.0f `p75DaysUnemp_5Years_Full'' \\ \addlinespace[0.2cm]" _newline ///
	"Days on DI (2y) & `: di %5.0f `DaysOnDI_2Years_Full'' & `: di %5.0f `p25DaysOnDI_2Years_Full'' & `: di %5.0f `p50DaysOnDI_2Years_Full'' & `: di %5.0f `p75DaysOnDI_2Years_Full'' \\" _newline ///
	"Days on DI (5y) & `: di %5.0f `DaysOnDI_5Years_Full'' & `: di %5.0f `p25DaysOnDI_5Years_Full'' & `: di %5.0f `p50DaysOnDI_5Years_Full'' & `: di %5.0f `p75DaysOnDI_5Years_Full'' \\ \addlinespace[0.2cm]" _newline ///
	"Unemp. Spells (5y) & `: di %5.1f `unemplSpells5Ybefore_Full'' & `: di %5.0f `p25unemplSpells5Ybefore_Full'' & `: di %5.0f `p50unemplSpells5Ybefore_Full'' & `: di %5.0f `p75unemplSpells5Ybefore_Full'' \\" _newline ///
	"Employers (5y) & `: di %5.1f `nEmployers5Y_Full'' & `: di %5.0f `p25nEmployers5Y_Full'' & `: di %5.0f `p50nEmployers5Y_Full'' & `: di %5.0f `p75nEmployers5Y_Full'' \\ \addlinespace[0.2cm]" _newline ///
	"Tenure (years) & `: di %5.1f `tenure_Full'' & `: di %5.0f `p25tenure_Full'' & `: di %5.0f `p50tenure_Full'' & `: di %5.0f `p75tenure_Full'' \\" _newline ///
	"Firm Size & `: di %5.0fc `L_nEmployees_L1L2_Full'' & `: di %5.0fc `p25L_nEmployees_L1L2_Full'' & `: di %5.0fc `p50L_nEmployees_L1L2_Full'' & `: di %5.0fc `p75L_nEmployees_L1L2_Full'' \\" _newline ///
	"Firm Layoff Rate & `: di %5.0f `L_layoffRate_L1L2_Full''\% & `: di %5.0f `p25L_layoffRate_L1L2_Full''\% & `: di %5.0f `p50L_layoffRate_L1L2_Full''\% & `: di %5.0f `p75L_layoffRate_L1L2_Full''\% \\" _newline ///
	///
	"\addlinespace[1.5ex]" _newline ///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	"\end{tabular}" _newline ///
	"% \end{center}" _newline ///
	"% \end{table}" _newline ///
	"% \end{document}"
file close myfile


